#include <iostream>
#include <fstream>
#include "contact.pb.h"

using namespace std;

void PrintContacts(contacts2::Contacts contacts)
{
    // for遍历所有联系人
    for (int i = 0; i < contacts.contact_size(); i++)
    {
        const ::contacts2::PeopleInfo &people = contacts.contact(i);
        cout << "联系人姓名：" << people.name() << endl;
        cout << "联系人年龄：" << people.age() << endl;
        for (int j = 0; j < people.phone_size(); j++)
        {
            const ::contacts2::PeopleInfo_Phone &phone = people.phone(i);
            cout << "联系人电话" << j + 1 << ":" << phone.num() << endl;
        }
    }
}

int main()
{
    contacts2::Contacts contacts;

    // 1. 将本地联系人文件读取进通讯录
    fstream input("contacts.bin", ios::in | ios::binary); // 如果打开成功就读入，打开失败会帮我新建文件

    // 1. 将本地联系人文件读取进通讯录
    if (!contacts.ParseFromIstream(&input))
    {
        cerr << "parse error!" << endl;
        input.close();
        return -1;
    }

    // 打印一下通讯录
    PrintContacts(contacts);
    return 0;
}